const validate = require('../../utils/vaildate')
import {register} from "../../service/auth";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    email: '',
    password: '',
    password_confirm: '',
    error_name: '',
    error_email: '',
    error_password: '',
    error_password_confirm: '',
    avatar:'https://img0.baidu.com/it/u=2382894529,3827550732&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1719334800&t=14a8be15af32d811c90b1d8ab0ee8046'
  },

  login() {
    const regName = validate.min(this.data.name,1)
    const regEmail = validate.email(this.data.email)
    const regPassword = validate.min(this.data.password,6)
    const regConfirm = validate.confirm(this.data.password,this.data.password_confirm)

    if (!regName) {
      this.setData({error_name: '昵称最小一个字符'})
    }
    else {this.setData({error_name: ''})
    }

    if (!regEmail) {
      this.setData({error_email: '邮箱格式不正确'})
    }
    else {this.setData({error_email: ''})
    }

    if (!regPassword) {
      this.setData({error_password: '密码最少6位'})
    }
    else {this.setData({error_password: ''})
    }

    if (!regConfirm) {
      this.setData({error_password_confirm: '两次密码不一致'})
    }
    else {this.setData({error_password_confirm: ''})
    }

    if (!regName || !regEmail || !regPassword || !regConfirm) return


    //用于注册方法的表单
    const data = {
      name: this.data.name,
      email: this.data.email,
      password: this.data.password,
      password_confirmation: this.data.password_confirm,
      openid:wx.getStorageSync('openid'),
      avatar: this.data.avatar
    }

    register(data).then(()=>{
      //重定向到登录页(一般真实项目，注册完会自动登录，需要api返回必须的数据，如token等)
        wx.redirectTo({
          url:'/pages/login/login'
        })
    })
  },
  handleWxUser(e){
    console.log(1)
    wx.getUserProfile({
      desc:'用于完善注册信息',  //申明获取用户的个人信息的用途后，后续会展示在弹窗中，请谨慎填写
      success:(res)=>{
        console.log(res);
        this.setData({
          name:res.userInfo.nickName,
          avatar:res.userInfo.avatarUrl
        })
      }
    })
  }
})